package upc.dew.inscripcionalumnos.controller;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import upc.dew.inscripcionalumnos.service.AdministradorService;
import upc.dew.inscripcionalumnos.service.CatalogoService;
import upc.dew.inscripcionalumnos.service.EvaluadorService;
import upc.dew.inscripcionalumnos.service.LoginService;
import upc.dew.inscripcionalumnos.service.PostulanteService;
import upc.dew.inscripcionalumnos.service.Service;

/**
 *
 * @author andres
 */
public class BaseController extends AbstractController
{	
    private static final String FILES_DIR = "files";

    private static final String FILES_SEPARATOR = "/";

    @Autowired
    private DateFormat dateFormat;

    @Autowired
    private Service service;
    
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return super.handleRequest(request, response);
    }

    /**
     * Obtiene la ruta donde se almacenar�n los archivos cargados.
     * 
     * @return
     */
    protected String getFilesDirectory()
    {
        return getTempDir().getParentFile().getAbsolutePath().concat(FILES_SEPARATOR).concat(FILES_DIR).concat(FILES_SEPARATOR);
    }

    /**
     * Obtiene la extension de un archivo segun el nombre completo del mismo.
     * 
     * @param filename
     * @return
     */
    protected String getFileExtension(String filename)
    {
        return filename.substring(filename.lastIndexOf(".") + 1, filename.length());
    }

    protected void createFilesDirectory(String directory)
    {
        File file = new File(getFilesDirectory().concat(directory));
        file.mkdir();
    }

    /**
     *
     * @param date
     * @return
     */
    protected Date safeParseDate(String date)
    {
        try
        {
            return dateFormat.parse(date);
        }
        catch (ParseException ex)
        {
            // No se pudo parsear la fecha
        }

        return new Date();
    }

    protected String formatDate(Date date)
    {
        return dateFormat.format(date);
    }

    /**
     *
     * @param request
     * @param key
     * @param obj
     */
    protected void setAttributeToSession(HttpServletRequest request, String key, Object obj)
    {
        request.getSession().setAttribute(key, obj);
    }

    /**
     * 
     * @param request
     * @param key
     * @return
     */
    protected Object getAttributeFromSession(HttpServletRequest request, String key)
    {
        return request.getSession().getAttribute(key);
    }
    
    /**
     * Obtiene la implementaci&oacute;n de PostulanteService.
     * 
     * @return
     */
    protected PostulanteService getPostulanteService()
    {
    	return service.getPostulanteService();
    }

    /**
     * Obtiene la implementaci&oacute;n de EvaluadorService.
     * 
     * @return
     */
    protected EvaluadorService getEvaluadorService()
    {
    	return service.getEvaluadorService();
    }
    
    /**
     * Obtiene la implementaci&oacute;n de AdministradorService.
     * 
     * @return
     */
    protected AdministradorService getAdministradorService()
    {
    	return service.getAdministradorService();
    }
    
    /**
     * Obtiene la implementaci&oacute;n de LoginService.
     * 
     * @return
     */
    protected LoginService getLoginService()
    {
    	return service.getLoginService();
    }
    
    /**
     * Obtiene la implementaci&oacute;n de CatalogoService.
     * 
     * @return
     */
    protected CatalogoService getCatalogoService()
    {
    	return service.getCatalogoService();
    }
}